package com.dingwei.lesyndrome.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.bumptech.glide.Glide;
import com.dingwei.lesyndrome.R;
import com.dingwei.lesyndrome.adapter.GamCirclePicAdapter;
import com.dingwei.lesyndrome.adapter.ReportAdapter;
import com.dingwei.lesyndrome.app.MyApplication;
import com.dingwei.lesyndrome.bean.CheckBean;
import com.dingwei.lesyndrome.bean.CircleDetailsBean;
import com.dingwei.lesyndrome.mvp.presenter.CircleReportPresenter;
import com.dingwei.lesyndrome.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.SpaceDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.lihao.baselibrary.weight.MyRadioGroup;
import com.lihao.baselibrary.weight.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created time 2018/3/6.
 * Creator 南柯一梦丶 on
 * Personal signature 余生还长 何必慌张
 * Page description :
 */

public class CircleReportActivity extends BaseActivity implements Contract.CircleReportView {

    CircleReportPresenter circleReportPresenter = new CircleReportPresenter(this);
    @Bind(R.id.title_bar)
    TitleBar titleBar;
    @Bind(R.id.img_head)
    ImageView imgHead;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_time)
    TextView tvTime;
    @Bind(R.id.tv_content)
    TextView tvContent;
    @Bind(R.id.grid_pics)
    RecyclerView gridPics;
    @Bind(R.id.img_top)
    ImageView imgTop;
    @Bind(R.id.fl_video)
    FrameLayout flVideo;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.et_reason)
    EditText etReason;
    @Bind(R.id.btn_submit)
    SuperButton btnSubmit;


    private CircleDetailsBean bean;
    private GamCirclePicAdapter adapter;
    private ReportAdapter reportAdapter;
    List<CheckBean> list = new ArrayList<>();

    @Override
    protected int getContentViewId() {
        return R.layout.activity_circle_report;
    }

    @Override
    protected void initData() {
        Glide.with(context)
                .load(bean.data.showHportrait)
                .apply(MyApplication.headOptions)
                .into(imgHead);
        tvName.setText(bean.data.showName);
        tvTime.setText(bean.data.showTime);
        tvContent.setText(bean.data.content);
        if (TextUtils.isEmpty(bean.data.video)) {
            flVideo.setVisibility(View.GONE);
        } else {
            flVideo.setVisibility(View.VISIBLE);
            Glide.with(BaseActivity.context)
                    .load(bean.data.vimg)
                    .apply(MyApplication.options)
                    .into(imgTop);
        }


        if (bean.data.imgList != null && bean.data.imgList.size() > 0) {
            gridPics.setVisibility(View.VISIBLE);
            adapter.clear();
            for (int i = 0; i < bean.data.imgList.size(); i++) {
                adapter.add(bean.data.imgList.get(i).showFile);
            }
        } else {
            gridPics.setVisibility(View.GONE);
        }
    }

    @Override
    protected void initView() {
        bean = (CircleDetailsBean) getIntent().getSerializableExtra("bean");


        adapter = new GamCirclePicAdapter(BaseActivity.context);
        GridLayoutManager layoutManager = new GridLayoutManager(BaseActivity.context, 4);
        layoutManager.setSpanSizeLookup(adapter.obtainGridSpanSizeLookUp(4));
        SpaceDecoration itemDecoration = new SpaceDecoration((int) DisplayUtil.dip2px(BaseActivity.context, 10));//参数是距离宽度
        itemDecoration.setPaddingEdgeSide(false);//是否为左右2边添加padding.默认true.
        itemDecoration.setPaddingStart(false);//是否在给第一行的item添加上padding(不包含header).默认true.
        itemDecoration.setPaddingHeaderFooter(false);//是否对Header于Footer有效,默认false.
        gridPics.setLayoutManager(layoutManager);
        gridPics.setAdapter(adapter);
        gridPics.setNestedScrollingEnabled(false);
        gridPics.addItemDecoration(itemDecoration);


        reportAdapter = new ReportAdapter(BaseActivity.context);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(BaseActivity.context, 4);
        gridLayoutManager.setSpanSizeLookup(adapter.obtainGridSpanSizeLookUp(4));
        SpaceDecoration itemDecoration1 = new SpaceDecoration((int) DisplayUtil.dip2px(BaseActivity.context, 16));//参数是距离宽度
        itemDecoration1.setPaddingEdgeSide(false);//是否为左右2边添加padding.默认true.
        itemDecoration1.setPaddingStart(false);//是否在给第一行的item添加上padding(不包含header).默认true.
        itemDecoration1.setPaddingHeaderFooter(false);//是否对Header于Footer有效,默认false.
        mRecyclerView.setLayoutManager(gridLayoutManager);
        mRecyclerView.setAdapter(reportAdapter);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(itemDecoration1);


        for (int i = 0; i < bean.data.reportTypeList.size(); i++) {
            CheckBean info = new CheckBean();
            info.name = bean.data.reportTypeList.get(i);
            list.add(info);
        }
        reportAdapter.addAll(list);

        reportAdapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                for (int i = 0; i < list.size(); i++) {
                    list.get(i).isCheck =false;
                }
                list.get(position).isCheck = true;
                reportAdapter.clear();
                reportAdapter.addAll(list);
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.fl_video, R.id.btn_submit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.fl_video:
                Intent intent = new Intent(BaseActivity.context, VideoPlayerActivity.class);
                intent.putExtra("url", bean.data.video);
                intent.putExtra("face", bean.data.vimg);
                startActivity(intent);
                break;
            case R.id.btn_submit:
                circleReportPresenter.reportCircle(bean.data.id, list, etReason.getText().toString());
                break;
        }
    }

    @Override
    public void reportCircleSuccess() {
        finish();
    }
}
